import {getTranslations, translate} from "./translations";
import xml2js from "xml2js";
import {promises as fs} from "fs";

const sourceBasePath = 'C:/X4';

const getContent = async (paths, type, translations, bullets) => {
    const result = {};

    await paths.reduce(async (prev, filePath) => {
        // before we start file operations, let's wait for previous step to finish
        await prev;

        if (filePath.indexOf('legacy') !== -1) return Promise.resolve();
        let size = null;
        if (filePath.indexOf('_s_') !== -1) size = 'small';
        else if (filePath.indexOf('_m_') !== -1) size = 'medium';
        else if (filePath.indexOf('_xl_') !== -1) size = 'extralarge';
        else if (filePath.indexOf('_l_') !== -1) size = 'large';
        else return Promise.resolve();

        const parser = new xml2js.Parser({mergeAttrs: true, explicitArray: false});
        const parsed = await parser.parseStringPromise(await fs.readFile(filePath));

        const data = parsed.macros.macro;
        const properties = parsed.macros.macro.properties;

        if (!data.properties) return Promise.resolve();

        if (!result[size]) result[size] = {};

        // for some reason all shields are referring to Argon ones...
        // also sometimes engines are also missing
        // if this gets fixed in EgoSoft file, then change id to data.component.ref
        // in meantime we can abuse name and just remove _macro part

        // [TODO]: Missiles don't have bullets
        if (type === 'turret' && data.class !== 'missileturret') {
            result[size][data.name.replace('_macro', '')] = {
                name: translate(properties.identification.name, translations, true),
                makerrace: properties.identification.makerrace,
                mk: properties.identification.mk,
                class: data.class,
                bullet: applyBulletInfo(bullets, properties.bullet.class),
                rotation: {
                    speed: properties.rotationspeed.max,
                },
                reload: {
                    rate: properties.reload.rate,
                    time: properties.reload.time,
                },
                hull: properties.hull.max,
            };
        } else if (type === 'turret' && data.class === 'missileturret') {
            result[size][data.name.replace('_macro', '')] = {
                name: translate(properties.identification.name, translations, true),
                makerrace: properties.identification.makerrace,
                mk: properties.identification.mk,
                class: data.class,
                //bullet: applyBulletInfo(bullets, properties.bullet.class),
                rotation: {
                    speed: properties.rotationspeed ? properties.rotationspeed.max : properties.rotationacceleration.max,
                },
                ammunition: properties.ammunition.tags.trim().split(' '),
                storage: properties.storage.capacity,
                hull: properties.hull.max,
            };
        } else if (type === 'weapon' && data.class !== 'missilelauncher') {
            if (data.name.replace('_macro', '').indexOf('_video') !== -1) return;
            result[size][data.name.replace('_macro', '')] = {
                name: translate(properties.identification.name, translations, true),
                heat: properties.heat,
                mk: properties.identification.mk,
                class: data.class,
                bullet: applyBulletInfo(bullets, properties.bullet.class),
                rotation: {
                    speed: properties.rotationspeed ? properties.rotationspeed.max : 'N/A',
                    acceleration: properties.rotationacceleration ? properties.rotationacceleration.max : 'N/A',
                },
                hull: properties.hull.max,
            };
        } else if (type === 'weapon' && data.class === 'missilelauncher') {
            result[size][data.name.replace('_macro', '')] = {
                name: translate(properties.identification.name, translations, true),
                mk: properties.identification.mk,
                class: data.class,
                // bullet: applyBulletInfo(bullets, properties.bullet.class),
                ammunition: properties.ammunition.tags.trim().split(' '),
                hull: properties.hull.max,
            };
        } else if (type === 'engine') {
            if (data.name.replace('_macro', '').indexOf('_video') !== -1) return;

            console.log(translations)

            const name = translate(properties.identification.name, translations, true)
            result[size][data.name.replace('_macro', '')] = {
                id: data.name.replace('_macro', ''),
                name: translate(properties.identification.name, translations, true).replace('APL', 'SPL'),
                mk: properties.identification.mk,
                class: type,
                makerrace: properties.identification.makerrace,
                thrust: properties.thrust,
                boost: properties.boost,
                travel: properties.travel,
            };
        } else if (type === 'thruster') {
            result[size][data.name.replace('_macro', '')] = {
                id: data.name.replace('_macro', ''),
                name: translate(properties.identification.name, translations, true),
                mk: properties.identification.mk,
                class: type,
                thrust: properties.thrust,
            };
        } else if (type === 'shield') {
            if (data.name.replace('_macro', '').indexOf('shield_gen_m_yacht_01_mk1') !== -1) return;
            if (data.name.replace('_macro', '').indexOf('m_standard_01') !== -1) return;

            result[size][data.name.replace('_macro', '')] = {
                id: data.name.replace('_macro', ''),
                name: translate(properties.identification.name, translations, true),
                description: translate(properties.identification.shortname, translations, true),
                mk: properties.identification.mk,
                class: type,
                makerrace: properties.identification.makerrace,
                recharge: {
                    max: properties.recharge.max,
                    rate: properties.recharge.rate,
                    delay: properties.recharge.delay,
                },
            };
        } else if (type === 'bullet') {
            result[data.name.replace('_macro', '')] = {
                class: data.class,
                ammunition: properties.ammunition,
                bullet: properties.bullet,
                heat: properties.heat,
                damage: properties.damage,
                reload: properties.reload,
                system: properties.weapon ? properties.weapon.system : null,
            };
        } else if (type === 'storage') {
            result[parsed.macros.macro.name] = {
                cargo: properties.cargo.max,
                type: properties.cargo.tags.trim(),
            };
        } else if (type === 'shipstorage') {
            result[parsed.macros.macro.name] = {
                name: translate(properties.identification.name, translations, true),
                capacity: properties.dock.capacity,
                type: properties.docksize.tags.trim(),
            };
        } else if (type === 'dockarea') {
            let pads_m = 0;
            let pads_s = 0;

            if (!Array.isArray(parsed.macros.macro.connections.connection))
                parsed.macros.macro.connections.connection = [parsed.macros.macro.connections.connection];
            parsed.macros.macro.connections.connection.forEach((connection) => {
                if (connection.ref.indexOf('_s_') !== -1) pads_s += 1;
                if (connection.ref.indexOf('_m_') !== -1) pads_m += 1;
            });

            result[parsed.macros.macro.name] = {
                name: translate(properties.identification.name, translations, true),
                pads_m,
                pads_s,
            };
        }
    }, Promise.resolve());

    return result;
};

async function test() {
    const translations = await getTranslations(sourceBasePath);

    // const engines = ['C:/x4/assets/props/Engines/macros/engine_arg_l_allround_01_mk1_macro.xml']

    // const res = await getContent(engines, 'engine', translations)

    const a = translations[20107].translations[1001]

    console.log(a)
}

test()